package com.topera.educms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topera.commonutils.R;
import com.topera.educms.entity.CrmBanner;
import com.topera.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-06-01
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    /**
     * 分页查询
     * @return
     */
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit){
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        bannerService.page(pageBanner,null);
        long total = pageBanner.getTotal();//总记录数
        List<CrmBanner> records = pageBanner.getRecords();//记录

        Map result = new HashMap();
        result.put("total",total);
        result.put("records",records);
        return R.ok().data(result);
    }

    /**
     * 新增
     * @param banner
     * @return
     */
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner banner){
        bannerService.save(banner);
        return R.ok();
    }

    /**
     * 查询指定的数据
     * @param id
     * @return
     */
    @GetMapping("getBanner/{id}")
    public R getBanner(@PathVariable String id){
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner",banner);
    }

    /**
     * 更新
     * @param banner
     * @return
     */
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner banner){
        bannerService.updateById(banner);
        return R.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteBanner/{id}")
    public R deleteBannner(@PathVariable String id){
        bannerService.removeById(id);
        return R.ok();
    }
}

